import java.util.ArrayList;
class ShoppingCart {
    private ArrayList<Product> products = new ArrayList<>();

    private int findProductIndex(String productName) {
        for (int i = 0; i < products.size(); i++) {
            if (products.get(i).getName().equals(productName)) {
                return i;
            }
        }
        return -1;
    }

    public void addProduct(Product product, int quantity) {
        if (quantity <= product.getQuantity()) {
            int index = findProductIndex(product.getName());
            if (index >= 0) {
                Product cartProduct = products.get(index);
                cartProduct.setQuantity(cartProduct.getQuantity() + quantity);
                product.setQuantity(product.getQuantity() - quantity);
            } else {
                Product newProduct = new Product(product.getName(), product.getType(), product.getPrice(), quantity);
                products.add(newProduct);
                product.setQuantity(product.getQuantity() - quantity);
            }
        } else {
            System.out.println("无法添加，已超出库存。");
        }
    }

    public void removeProduct(String productName) {
        int index = findProductIndex(productName);
        if (index >= 0) {
            products.remove(index);
        } else {
            System.out.println("购物车中没有找到该商品。");
        }
    }

    public void viewCart() {
        if (products.isEmpty()) {
            System.out.println("购物车为空。");
        } else {
            System.out.println("商品名称\t"+"类型\t"+"价格\t"+"数量\t");
            for (Product product : products) {
                System.out.println( product.getName()+"\t\t"+product.getType()+"\t"+product.getPrice()+"\t"+product.getQuantity());
            }
        }
    }

    public double calculateTotal() {
        double total = 0.0;
        for (Product product : products) {
            total += product.getPrice() * product.getQuantity();
        }
        return total;
    }
}